From: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 10 Apr 2020 20:25:46 +0000 (+0200)
Subject: ttyread: test for EOF while reading tty
X-Git-Url: https://git.owens.tech/assets/lich_lifts_title_slice.png%20%22Lich%20Lifts%22/assets/lich_lifts_title_slice.png%20%22Lich%20Lifts%22/git?a=commitdiff_plain;h=e52319cc7d153e4f59b38c4fb4c0556e118d4775;p=st.git

ttyread: test for EOF while reading tty

When a read operation returns 0 then it means that we arrived to the end of the
file, and new reads will return 0 unless you do some other operation such as
lseek(). This case happens with USB-232 adapters when they are unplugged.
---

diff --git a/st.c b/st.c
index 5f2352a..81973ee 100644
--- a/st.c
+++ b/st.c
@@ -823,17 +823,24 @@ ttyread(void)
 	int ret;
 
 	/* append read bytes to unprocessed bytes */
-	if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
-		die("couldn't read from shell: %s\n", strerror(errno));
-	buflen += ret;
+	ret = read(cmdfd, buf+buflen, LEN(buf)-buflen);
 
-	written = twrite(buf, buflen, 0);
-	buflen -= written;
-	/* keep any uncomplete utf8 char for the next call */
-	if (buflen > 0)
-		memmove(buf, buf + written, buflen);
+	switch (ret) {
+	case 0:
+		fputs("Found EOF in input\n", stderr);
+		exit(0);
+	case -1:
+		die("couldn't read from shell: %s\n", strerror(errno));
+	default:
+		buflen += ret;
+		written = twrite(buf, buflen, 0);
+		buflen -= written;
+		/* keep any uncomplete utf8 char for the next call */
+		if (buflen > 0)
+			memmove(buf, buf + written, buflen);
+		return ret;
 
-	return ret;
+	}
 }
 
 void